package com.songcha.library_common.util

import android.os.Handler
import android.os.Looper
import android.view.Gravity
import android.widget.Toast
import com.songcha.library_common.LibraryCommon


class ToastUtil {
    companion object{
        const val TOAST_INTERVAL=3000
        private var lastToast=""
        private var lastToastTime=0L

        @JvmStatic
        fun show(msg:Any?,gravity:Int?=null){
            if(msg==null) return

            if(lastToast==msg.toString() && System.currentTimeMillis()-lastToastTime<TOAST_INTERVAL){
                return
            }else{
                lastToast=msg.toString()
                lastToastTime=System.currentTimeMillis()
            }

            Handler(Looper.getMainLooper()).post{
                var toast:Toast
                if(msg is String){
                    toast=Toast.makeText(LibraryCommon.getContext(),msg, Toast.LENGTH_LONG)
                } else{
                    toast=Toast.makeText(LibraryCommon.getContext(),msg.toString(), Toast.LENGTH_LONG)
                }
                if(gravity!=null)
                    toast.setGravity(gravity,0,0)
                toast.show()
            }

        }

    }

}